package com.salts.service.Impl;

import com.salts.Dao.CategoryMapper;
import com.salts.Dao.ClothMapper;
import com.salts.domain.po.Cloth;
import com.salts.domain.po.ClothCategory;
import com.salts.domain.vo.DisplayCategoryVo;
import com.salts.domain.vo.DisplayClothDetailVo;
import com.salts.domain.vo.DisplayClothVo;
import com.salts.service.HomePageService;
import com.salts.service.SortService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class SortServiceImpl implements SortService {
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    ClothMapper clothMapper;
    @Autowired
    HomePageService homePageService;
    //缓存类别信息
    private List<Map<String, List<String>>> categoryInfoCache = null;
//返回值为List<DisplayCategoryVo>,
    List<Map<String,List<String>>> getCategoryInfo(){
        List<Map<String, List<String>>> returnDto = new ArrayList<>();
        if (categoryInfoCache == null) {
            HashMap<String, List<String>> allInfo = new HashMap<>();
            List<DisplayCategoryVo> categoryAllInfo = categoryMapper.getCategoryAllInfo();
            Map<String, List<DisplayCategoryVo>> categoryMap = categoryAllInfo.stream()
                    .collect(Collectors.groupingBy(DisplayCategoryVo::getCategoryTre));
            for (Map.Entry<String, List<DisplayCategoryVo>> entry : categoryMap.entrySet()) {
                Map<String, List<String>> categorys = new HashMap<>();
                for (DisplayCategoryVo categoryVo : entry.getValue()) {
                    String categoryOne = categoryVo.getCategoryOne();
                    String categoryTwo = categoryVo.getCategoryTwo();
                    categorys.computeIfAbsent(categoryOne, k -> new ArrayList<>()).add(categoryTwo);//这句话整理categoryOne,把categoryTwo收集成List返回
                }
                returnDto.add(categorys);
            }
        }
            return returnDto;
    }
    @Override
    public List<List<String>> getCategoryOne() {
        if (categoryInfoCache==null){
            categoryInfoCache=getCategoryInfo();
        }
        List<List<String>> categoryOne = new ArrayList<>();
        for (Map<String, List<String>> sexCategory : categoryInfoCache) {
            List<String> strings = new ArrayList<>(sexCategory.keySet());
            categoryOne.add(strings);
        }
        //categoryInfoCache.get();
        return categoryOne;
    }

    @Override
    public List<String> getCategoryOneBySex(String sex) {
        List<List<String>> categoryOne = getCategoryOne();
        List<String> strings=null;
        if (sex.equals("女")){
            strings = categoryOne.get(0);
        }if(sex.equals(("男"))){
            strings = categoryOne.get(1);
        }
        return strings;
    }

    @Override
    public List<String> getCategoryTwo(String categoryTre,String categoryOne) {
        if (categoryInfoCache==null){
            categoryInfoCache=getCategoryInfo();
        }
        List<String> strings = null;
        if (categoryTre.contains("女")){
            Map<String, List<String>> stringListMap = categoryInfoCache.get(0);
            strings = stringListMap.get(categoryOne);
        }else {
            Map<String, List<String>> stringListMap = categoryInfoCache.get(1);
            strings = stringListMap.get(categoryOne);
        }
        return strings;
    }

    @Override
    public List<List<String>> getCategoryTre() {
        if (categoryInfoCache==null){
            categoryInfoCache=getCategoryInfo();
        }
        return null;//暂时不需要
    }
//todo 正在做
    @Override
    public List<DisplayClothVo> getCategoryProduct(ClothCategory category) {
        List<Integer> ClothIds=categoryMapper.getClothIds(category.getCategoryOne(),category.getCategoryTwo(),category.getCategoryTre());
        return clothMapper.getProductInfoById(ClothIds);
    }

    /**
     * @param clothName
     * @return
     */
    @Override
    public List<DisplayClothVo> getProduct(String clothName) {
        List<Cloth> cloths = clothMapper.queryByName(clothName);
        List<DisplayClothVo> displayClothVos = new ArrayList<>();
        for (Cloth cloth : cloths) {
            DisplayClothVo displayClothVo = new DisplayClothVo();
            displayClothVo.setClothId(cloth.getId());
            displayClothVo.setClothName(cloth.getClothName());
            displayClothVo.setPrice(cloth.getPrice());
            displayClothVo.setComment(cloth.getComment());
            displayClothVo.setImgUrl(cloth.getImageUrl());
            displayClothVos.add(displayClothVo);

        }
        return displayClothVos;
    }


}
